<template>
	<view class="container">
		<view class="header">
			<text class="title">我的备忘录</text>
			<view class="header-actions">
				<view class="draw-btn" @click="goToDraw">
					<text class="draw-icon">🖊</text>
				</view>
				<view class="add-btn" @click="showAddModal = true">
					<text class="add-icon">+</text>
				</view>
			</view>
		</view>

		<scroll-view class="task-list" scroll-y>
			<view class="task-item" v-for="(task, index) in tasks" :key="index">
				<checkbox :checked="task.completed" @click="toggleTask(index)" />
				<view class="task-content-wrapper">
					<text class="task-content" :class="{ 'completed': task.completed }">
						{{ task.type === 'drawing' ? '绘画笔记' : task.content }}
					</text>
					<image v-if="task.image" :src="task.image" mode="widthFix" class="task-image"
						@click="previewImage(task.image)"></image>
				</view>
				<view class="task-actions">
					<text class="action-btn" @click="editTask(index)" v-if="task.type !== 'drawing'">编辑</text>
					<text class="action-btn delete" @click="deleteTask(index)">删除</text>
				</view>
			</view>
		</scroll-view>

		<!-- 添加/编辑任务弹窗 -->
		<view class="modal" v-if="showAddModal">
			<view class="modal-content">
				<view class="modal-header">
					<text class="modal-title">{{ editingIndex === null ? '添加任务' : '编辑任务' }}</text>
					<text class="close-btn" @click="closeModal">×</text>
				</view>
				<view class="modal-body">
					<input type="text" v-model="currentTask.content" placeholder="请输入任务内容" />
					<view class="image-preview" v-if="currentTask.image">
						<image :src="currentTask.image" mode="widthFix" class="preview-image"></image>
						<text class="delete-image" @click="removeImage">×</text>
					</view>
					<view class="modal-actions">
						<button @click="importImage">导入图片</button>
						<button @click="saveTask">保存</button>
						<button @click="closeModal">取消</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tasks: [],
				showAddModal: false,
				editingIndex: null,
				currentTask: {
					content: '',
					completed: false,
					image: '',
					type: 'normal'
				}
			};
		},
		onLoad() {
			this.loadTasks();
		},
		onShow() {
			this.loadTasks();
		},
		methods: {
			loadTasks() {
				// 从本地存储加载任务
				const savedTasks = uni.getStorageSync('tasks');
				if (savedTasks) {
					this.tasks = JSON.parse(savedTasks);
				} else {
					this.tasks = [];
				}
			},
			toggleTask(index) {
				this.tasks[index].completed = !this.tasks[index].completed;
				this.saveTasks();
			},
			addTask() {
				this.editingIndex = null;
				this.currentTask = {
					content: '',
					completed: false,
					image: '',
					type: 'normal'
				};
				this.showAddModal = true;
			},
			editTask(index) {
				if (this.tasks[index].type === 'drawing') return;
				this.editingIndex = index;
				this.currentTask = { ...this.tasks[index] };
				this.showAddModal = true;
			},
			deleteTask(index) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这个任务吗？',
					success: res => {
						if (res.confirm) {
							this.tasks.splice(index, 1);
							this.saveTasks();
						}
					}
				});
			},
			saveTask() {
				if (this.currentTask.content.trim()) {
					if (this.editingIndex === null) {
						this.tasks.push({ ...this.currentTask });
					} else {
						this.tasks[this.editingIndex] = { ...this.currentTask };
					}
					this.saveTasks();
					this.closeModal();
				}
			},
			closeModal() {
				this.showAddModal = false;
				this.editingIndex = null;
				this.currentTask = {
					content: '',
					completed: false,
					image: '',
					type: 'normal'
				};
			},
			saveTasks() {
				uni.setStorageSync('tasks', JSON.stringify(this.tasks));
			},
			goToDraw() {
				uni.navigateTo({ url: '/pages/draw/index' });
			},
			importImage() {
				uni.chooseImage({
					count: 1,
					success: res => {
						const tempFilePath = res.tempFilePaths[0];
						// 将临时文件保存到本地
						uni.saveFile({
							tempFilePath: tempFilePath,
							success: saveRes => {
								this.currentTask.image = saveRes.savedFilePath;
							},
							fail: err => {
								console.error('保存文件失败:', err);
								uni.showToast({
									title: '图片导入失败',
									icon: 'none'
								});
							}
						});
					},
					fail: err => {
						console.error('选择图片失败:', err);
						uni.showToast({
							title: '选择图片失败',
							icon: 'none'
						});
					}
				});
			},
			removeImage() {
				this.currentTask.image = '';
			},
			previewImage(url) {
				uni.previewImage({
					urls: [url],
					current: url
				});
			}
		}
	};
</script>

<style>
	.container {
		--ear-height: 40px;
		width: 100%;
		height: 100%;
		padding: 20rpx;
		background: url("/static/bc2.png") no-repeat center bottom;
		background-size: cover;
		min-height: 100vh;
		box-sizing: border-box;
	}

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
		background-color: rgba(255, 255, 255, 0.6);
		border-radius: 10px;
		padding: 20px;
	}

	.title {
		font-size: 40rpx;
		color: #333;
		font-weight: bold;
	}

	.header-actions {
		display: flex;
		gap: 20rpx;
	}

	.draw-btn {
		width: 80rpx;
		height: 80rpx;
		background-color: #CCC;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.draw-icon {
		font-size: 40rpx;
	}

	.add-btn {
		width: 80rpx;
		height: 80rpx;
		background-color: #CCC;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.add-icon {
		color: #fff;
		font-size: 40rpx;
	}

	.task-list {
		height: calc(100vh - 200rpx);
	}

	.task-item {
		position: relative;
		display: flex;
		align-items: flex-start;
		padding: 20rpx;
		background-color: rgba(255, 255, 255, 0.6);
		border-radius: 10rpx;
		margin: 50px auto;
		z-index: 10px;
	}

	.task-item::before {
		content: '';
		position: absolute;
		width: 20px;
		height: var(--ear-height);
		background-color: rgba(255, 255, 255, 0.6);
		right: 30px;
		top: -40px;
		border-radius: 0 100% 0 100%;
	}

	.task-item::after {
		content: '';
		position: absolute;
		width: 20px;
		height: var(--ear-height);
		background-color: rgba(255, 255, 255, 0.6);
		right: 10px;
		top: -40px;
		border-radius: 100% 0 100% 0;
	}

	.task-content-wrapper {
		flex: 1;
		margin: 0 20rpx;
	}

	.task-content {
		font-size: 32rpx;
		word-break: break-all;
	}

	.task-content.completed {
		text-decoration: line-through;
		color: #999;
	}

	.task-image {
		width: 100%;
		margin-top: 20rpx;
		border-radius: 10rpx;
	}

	.task-actions {
		display: flex;
		gap: 20rpx;
	}

	.action-btn {
		color: #4CAF50;
		font-size: 28rpx;
	}

	.action-btn.delete {
		color: #f44336;
	}

	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.modal-content {
		background-color: #fff;
		width: 80%;
		border-radius: 20rpx;
		padding: 30rpx;
	}

	.modal-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.modal-title {
		font-size: 36rpx;
		font-weight: bold;
	}

	.close-btn {
		font-size: 40rpx;
		color: #999;
	}

	.modal-body input {
		width: 100%;
		height: 80rpx;
		border: 2rpx solid #ddd;
		border-radius: 10rpx;
		padding: 0 20rpx;
		margin-bottom: 30rpx;
	}

	.image-preview {
		position: relative;
		margin-bottom: 30rpx;
	}

	.preview-image {
		width: 100%;
		border-radius: 10rpx;
	}

	.delete-image {
		position: absolute;
		top: -20rpx;
		right: -20rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: #f44336;
		color: #fff;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}

	.modal-actions {
		display: flex;
		justify-content: flex-end;
		gap: 20rpx;
	}

	.modal-actions button {
		padding: 20rpx 40rpx;
		border-radius: 10rpx;
		font-size: 28rpx;
	}

	.modal-actions button:first-child {
		background-color: #4CAF50;
		color: #fff;
	}

	.modal-actions button:last-child {
		background-color: #f5f5f5;
		color: #333;
	}
</style>